#include "TriangleMesh.h"

TriangleMesh::TriangleMesh(void)
{
	numVert_ = 0;
	numFace_ = 0;
}

TriangleMesh::~TriangleMesh(void)
{
}

// Add vertex in 3D
void TriangleMesh::addVertex(float x, float y, float z)
{
	vertices_.push_back(x);
	vertices_.push_back(y);
	vertices_.push_back(z);
	numVert_++;
}

// Add triangle face to the mesh
void TriangleMesh::addFace(int v1, int v2, int v3)
{
	// Add the face
	faces_.push_back(v1);
	faces_.push_back(v2);
	faces_.push_back(v3);
	numFace_++;
}

int TriangleMesh::getNumVert()
{
	return numVert_;
}

int TriangleMesh::getNumFace()
{
	return numFace_;
}

float * TriangleMesh::getVertex(int id, float *vert) 
{
	//float vert[3] = {0.0f, 0.0f, 0.0f};
	if(id <= numVert_)
	{
			vert[0] = vertices_.at( (id - 1) * 3 );
			vert[1] = vertices_.at( (id - 1) * 3 + 1 );
			vert[2] = vertices_.at( (id - 1) * 3 + 2 );
	}
	return vert;
}

float TriangleMesh::getVertCoord(int id, int coord) 
{
	return vertices_.at( (id - 1) * 3 + coord );;
}


int* TriangleMesh::getFace(int id, int *face)
{
	//int face[3] = {0, 0 , 0};
	if(id <= numFace_)
	{
		if(id != 0)
		{
			face[0] = faces_.at( (id - 1) * 3 );
			face[1] = faces_.at( (id - 1) * 3 + 1);
			face[2] = faces_.at( (id - 1) * 3 + 2);
		} else
		{
			face[0] = faces_.at( 0 );
			face[1] = faces_.at( 1 );
			face[2] = faces_.at( 2 );
		}
	}
	return face;
}


void TriangleMesh::setFaces(const vector<int>& faces) {
	faces_ = faces;
}

vector<int> TriangleMesh::getFaces() {
	return faces_;
}

vector<float> TriangleMesh::getVertices() {
	return vertices_;
}

void TriangleMesh::clear() {
}

void TriangleMesh::buildEdgeList() {
	for(int i = 0; i < numFace_; i++) {
		int emptyF[3] = {0, 0, 0};
		int * face_vert = getFace(i, emptyF);
		edge e1 = {face_vert[0], face_vert[1]};
		edge e2 = {face_vert[0], face_vert[2]};
		edge e3 = {face_vert[1], face_vert[2]};
		edges_.addEdge(e1);
		edges_.addEdge(e2);
		edges_.addEdge(e3);
	}
}

int TriangleMesh::sizeTri() {
	return (int)faces_.size();
}

int TriangleMesh::sizeGeom() {
	return (int)vertices_.size();
}
